//
//  FirstSpecialViewController.m
//  Project_B
//
//  Created by 张闯的mac pro on 15/11/28.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "FirstSpecialViewController.h"
#import "AFNetworkHandler.h"
#import "FirstModel.h"
#import "FirstSpecialTableViewCell.h"
#import "FirstTopViewViewController.h"
#import "UIImageView+WebCache.h"
#import "CalculateHeight.h"
#import "MBProgressHUD.h"
#import "UIImage+GIF.h"
#import "TCAllTableViewCell.h"

@interface FirstSpecialViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *specialTableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableDictionary *dataDic;
@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIView *headView;

@end

@implementation FirstSpecialViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"专题";
    [self getTableViewData];
//    [self createSpecialTableView];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:NO];

    self.hud.animationType = MBProgressHUDAnimationFade;
}



- (void)getTableViewData
{
    //http://api.lis99.com/v2/club/topicSpecialList/1302/0/
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v2/club/topicSpecialList/%@/0/", self.idString];
    [AFNetworkHandler getUrl:str cookie:nil result:^(id result) {
        
        self.dataDic = result[@"data"][@"specialinfo"];
        [self createSpecialTableView];
        
        NSMutableArray *array = result[@"data"][@"topiclist"];
        self.dataArr = array;
        
        [self.specialTableView reloadData];
        [self.hud removeFromSuperview];
        
    }];
}

- (void)createSpecialTableView
{
    CGFloat h = [CalculateHeight heightWithText:self.dataDic[@"describe"] fontSize:14];
    
    self.headView = [[UIView alloc] init];
    self.headView.frame = CGRectMake(0, 0, 375, h + 30);
    [self.view addSubview:self.headView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(375 / 2 - 15, 10, 50, 20)];
    titleLabel.text = @"导语";
    titleLabel.textColor = [UIColor colorWithRed:60.0 / 255 green:149.0 / 255 blue:58.0 / 255 alpha:1];
    [self.headView addSubview:titleLabel];
    
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.frame = CGRectMake(5, 25, 365, h);
    self.contentLabel.textColor = [UIColor grayColor];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.text = self.dataDic[@"describe"];
    [self.contentLabel setFont:[UIFont systemFontOfSize:13.5]];
    [self.headView addSubview:self.contentLabel];
    
    
   
    
    self.specialTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.specialTableView];
    self.specialTableView.delegate = self;
    self.specialTableView.dataSource = self;
    [self.specialTableView registerClass:[TCAllTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.specialTableView.tableHeaderView = self.headView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TCAllTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.dic = self.dataArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 210;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    FirstTopViewViewController *topVC = [[FirstTopViewViewController alloc] init];
    topVC.idString = self.dataArr[indexPath.row][@"topic_id"];
    [self.navigationController pushViewController:topVC animated:YES];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
